﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CourseArraging.Common;
using System.Text.RegularExpressions; 
using Stone.DataContracts;
using Stone.Library.Common;

namespace CourseArraging.UControl
{
    public partial class UCkcb : UCcurriculum
    {
        #region 参数
        private int x_coor;//存储cellchanged的x坐标
        private int y_coor;//存储cellchanged的y坐标
        private string course_cell_data;//存储时间格式1-2-15:30-17:30
        private List<string> _course_time_string = new List<string>();

        public List<string> course_time_string = new List<string>();//1-15:30-17:30
        public List<string> course_time_int = new List<string>();//将时间转成节次的形式. 1-8:00-10:00=>11
        public List<string> course_sec_list
        {
            get
            {
                _course_time_string.Clear();
                foreach (string s in course_time_string)
                {
                    string[] tmp = s.Split('-');
                    int tmpt = int.Parse(tmp[1].Split(':')[0]);
                    if (tmpt < 12)
                        _course_time_string.Add(s.Substring(0, 1) + "1");
                    else if (tmpt < 18)
                        _course_time_string.Add(s.Substring(0, 1) + "2");
                    else
                        _course_time_string.Add(s.Substring(0, 1) + "3");
                }
                return _course_time_string;
            }
        }
        public Zap2Entity zapEntity
        {
            get
            {
                return CommonHelper.int2zap2(course_time_int);
            }
        }

        #region 时间参数
        private string[] am_time_sec = { "8:00-10:00", "10:00-12:00" };
        private string[] pm_time_sec = { "13:00-15:00", "15:00-17:00" };
        private string[] eve_time_sec = { "18:00-20:00", "20:00-22:00" };
        #endregion

        //mlh 2011-03-29 add
        public bool ClickEnable = false; //用于判断是否支持点击进入签订临时安排界面
        //点击cell事件
        public delegate void ClickEventHandle(string str);
        public event ClickEventHandle ClickEvent;
        #endregion

        #region 构造器

        public UCkcb()
        {
            InitializeComponent();
            dataGrid1.CurrentCellChanged += new EventHandler(dataGrid1_CurrentCellChanged);
        }

        #endregion

        #region 事件

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            tscombo_time.Items.Clear();
            x_coor = this.dataGrid1.CurrentCell.ColumnNumber - 1;
            y_coor = this.dataGrid1.CurrentCell.RowNumber + 1;
            //1-5-15:30-17:30
            course_cell_data = x_coor + "-" + GetCellText(x_coor, y_coor);
            #region 区间判断
            int timesec = this.dataGrid1.CurrentCell.RowNumber;
            if (timesec == 0)
            {
                tscombo_time.Text = am_time_sec[0];
                tscombo_time.Items.AddRange(am_time_sec);
            }
            else if (timesec ==1)
            {
                tscombo_time.Text = am_time_sec[1];
                tscombo_time.Items.AddRange(am_time_sec);
            }
            else if (timesec == 2)
            {
                tscombo_time.Text = pm_time_sec[0];
                tscombo_time.Items.AddRange(pm_time_sec);
            }
            else if (timesec ==3)
            {
                tscombo_time.Text = pm_time_sec[1];
                tscombo_time.Items.AddRange(pm_time_sec);
            }
            else if (timesec == 4)
            {
                tscombo_time.Text = eve_time_sec[0];
                tscombo_time.Items.AddRange(eve_time_sec);
            }
            else if (timesec == 5)
            {
                tscombo_time.Text = eve_time_sec[1];
                tscombo_time.Items.AddRange(eve_time_sec);
            }

            if (GetCellText(x_coor, y_coor).ToString() == string.Empty && this.toolStrip1.Visible == false&&ClickEnable)
            {
                int x = this.dataGrid1.CurrentCell.RowNumber+1;
                int y = this.dataGrid1.CurrentCell.ColumnNumber - 1;
                string kcsj = string.Empty;
                if (x == 1)
                    kcsj = y + "-" + am_time_sec[0];
                else if (x == 2)
                    kcsj = y + "-" + am_time_sec[1];
                else if (x == 3)
                    kcsj = y + "-" + pm_time_sec[0];
                else if (x == 4)
                    kcsj = y + "-" + pm_time_sec[1];
                else if (x == 5)
                    kcsj = y + "-" + eve_time_sec[0];
                else if (x == 6)
                    kcsj = y + "-" + eve_time_sec[1];
                if (ClickEvent != null)
                    ClickEvent(kcsj);
            }
            #endregion
        }

        private void tsb_ok_Click(object sender, EventArgs e)
        {
            //ok  重写
            if (string.IsNullOrEmpty(tscombo_time.Text))
                return;
            string tmp = GetCellText(x_coor, y_coor);
            if (!string.IsNullOrEmpty(GetCellText(x_coor, y_coor)))
                tsb_del_Click(new object(), new EventArgs());
            if (validateTimeData())
            {
                //显示到表格上
                this.courseTable.SetCellData(x_coor + 1, y_coor - 1, null,
                    tscombo_time.Text);
                course_cell_data = x_coor + "-" + tscombo_time.Text;
                //添加时间到本类变量course_time_string和course_time_int中
                this.course_time_string.Add(course_cell_data);
                this.course_time_int.Add(CommonHelper.course2simple(course_cell_data));
            }
        }

        private void tsb_del_Click(object sender, EventArgs e)
        {
            //del 重写
            if (string.IsNullOrEmpty(GetCellText(x_coor, y_coor))) return;
            //**修改;减少y字段冗余**
            //string tmp = x_coor + "-" + (y_coor / 3 + 1) + "-" + GetCellText(x_coor, y_coor).ToString();
            string tmp1 = x_coor + "-" + GetCellText(x_coor, y_coor).ToString();
            string tmp2 = CommonHelper.course2simple(course_cell_data);
            course_time_string.Remove(tmp1);
            course_time_int.RemoveAll(x => x == tmp2);
            DelCellData(x_coor, y_coor);
        }

        #endregion

        #region 方法

        //初始化表格
        public void initeDataGrid()
        {
            x_coor = 0;
            y_coor = 0;
            course_cell_data = null;
            course_time_string.Clear();
            course_time_int.Clear();
            this.courseTable.ClearCourseData();
            ClickEnable = false;  
        }

        //验证时间数据
        private bool validateTimeData()
        {
            //string strRegex = @"^([0-9]|([0-1][0-9])|(2[0-3])):([0-9]|([0-5][0-9]))$";
            //Regex re = new Regex(strRegex);
            //if (!re.IsMatch(tscombo_time.Text) || !re.IsMatch(tscombo_end.Text))
            //{
            //    MessageBox.Show("时间格式填写错误，请重新填写！", "错误");
            //    return false;
            //}
            //if (CommonHelper.string2Int3length(tscombo_end.Text)
            //    <= CommonHelper.string2Int3length(tscombo_time.Text))
            //{
            //    MessageBox.Show("结束时间小于开始时间，请重新填写！", "错误");
            //    return false;
            //}
            //1-15:30-17:30=>11530-11730
            string course_data = CommonHelper.course2simple(x_coor + "-"
                + tscombo_time.Text);
            foreach (string di in course_time_int)
            {
                if (di == course_data)
                {
                    MessageBox.Show("上课时间重合，请重新填写上课时间！");
                    return false;
                }
            }
            return true;
        }

        //设置标题
        public void setCaption(string caption_name)
        {
            courseTable.SetCaptionText(caption_name);
        }

        //获取表格文本
        public string GetCellText(int x_coord, int y_coord)
        {
            return courseTable.GetCellText(x_coord + 1, y_coord - 1);
        }

        //删除表格内容
        public void DelCellData(int x_coord, int y_coord)
        {
            courseTable.DelCellData(x_coord + 1, y_coord - 1);
        }

        //设置表格内容
        public void SetCellData(int day, int daySect, object data, string courseName)
        {
            string s = GetCellText(day, daySect);
            if (!string.IsNullOrEmpty(GetCellText(day, daySect)))
            {
                SetCellData(day, daySect + 1, data, courseName);
            }
            else
            {
                //**修改,**
                //courseTable.SetCellData(day + 1, daySect*3-3, data, courseName);
                //this.course_time_string.Add(day+"-"+daySect+"-"+courseName);
                //this.course_time_int.Add(new DoubleInt
                //    (CommonHelper.string2string11length(day + "-" + daySect + "-" + courseName)));
                courseTable.SetCellData(day + 1, daySect - 1, data, courseName);
                this.course_time_string.Add(day + "-" + courseName);
                this.course_time_int.Add(CommonHelper.course2simple(day + "-" + courseName));
            }
        }

        //隐藏时间设置栏
        public void FadeTimeSet()
        {
            this.toolStrip1.Visible = false;
        }

        #endregion
    }
}
